package com.htgd.gardenexpo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.dao.GeConTheaterMovieBlessMapper;
import com.htgd.gardenexpo.dto.MovieBlessDTO;
import com.htgd.gardenexpo.entity.GeConTheaterMovieBless;
import com.htgd.gardenexpo.entity.GeConTheaterPlan;
import com.htgd.gardenexpo.page.MovieBlessQuery;
import com.htgd.gardenexpo.service.GeConTheaterMovieBlessService;
import com.htgd.gardenexpo.utils.TokenUtil;
import com.htgd.gardenexpo.vo.GeConTheaterMovieBlessVO;
import com.htgd.gardenexpo.vo.GeConTheaterPlanVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author: 王秀田
 * @date: 2024/11/12 18:19
 * @description: TODO
 */
@Service
public class GeConTheaterMovieBlessServiceImpl extends ServiceImpl<GeConTheaterMovieBlessMapper, GeConTheaterMovieBless> implements GeConTheaterMovieBlessService {

   @Autowired
   private GeConTheaterMovieBlessMapper geConTheaterMovieBlessMapper;


    @Override
    public PageInfo<GeConTheaterMovieBlessVO> pageList(MovieBlessQuery movieBlessQuery) {
        PageHelper.startPage(movieBlessQuery.getPageNum(), movieBlessQuery.getPageSize());
        List<GeConTheaterMovieBlessVO> list = geConTheaterMovieBlessMapper.pageList(movieBlessQuery);
        if(!list.isEmpty()){
            list.stream().forEach(item->{
                if(Objects.equals(item.getStatus(),"0")){
                    item.setStatusText("待审核");
                }else if(Objects.equals(item.getStatus(),"1")){
                    item.setStatusText("已通过");
                }else{
                    item.setStatusText("未通过");
                }
            });
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<GeConTheaterMovieBlessVO> blessList(MovieBlessQuery movieBlessQuery, HttpServletRequest request) {
        PageHelper.startPage(movieBlessQuery.getPageNum(), movieBlessQuery.getPageSize());
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        movieBlessQuery.setOpenId(openId);
        List<GeConTheaterMovieBlessVO> list = geConTheaterMovieBlessMapper.blessList(movieBlessQuery);
        return new PageInfo<>(list);
    }

    @Transactional
    @Override
    public synchronized void add(MovieBlessDTO movieBless, HttpServletRequest request) {
        String accessToken = request.getHeader("Access_token");
        String openId = TokenUtil.getUsernameFromToken(accessToken);
        GeConTheaterMovieBless geConTheaterMovieBless = new GeConTheaterMovieBless();
        BeanUtils.copyProperties(movieBless,geConTheaterMovieBless);
        geConTheaterMovieBless.setOpenId(openId);
        geConTheaterMovieBless.setCreateTime(LocalDateTime.now());
        geConTheaterMovieBlessMapper.insert(geConTheaterMovieBless);
    }

    @Override
    public GeConTheaterMovieBlessVO getGeConTheaterMovieBlessVOById(String id) {
        return geConTheaterMovieBlessMapper.getGeConTheaterMovieBlessVOById(id);
    }
}
